<template>
	<view>
		<view class="">
			<u-dropdown ref="uDropdown">
				<u-dropdown-item :title="title1" :options="options1" v-model="selCla" @change="changeCLass($event)">
				</u-dropdown-item>
			</u-dropdown>
		</view>
		<uni-calendar :clearDate="true" :showMonth="false" :insert="true" :selected="selected" :lunar="false"
			:start-date="'1990-01-01'" :end-date="'2099-01-01'" @monthSwitch="monthSwitch" @change="dateChange">
		</uni-calendar>
		<view v-for="(item,index) in courseList" :key="index">
			<uni-card :title="item.title" :extra="item.addrs">
				<text class="uni-body" v-html="item.content" style="line-height: 60px;"></text>
				<uni-list>
					<uni-list-item :title="item.attendClassTime"></uni-list-item>
					<uni-list-item :title="item.quittingTimeText"></uni-list-item>
				</uni-list>
				<!-- courseInfo.professorId && courseInfo.isEvaluate != 1 -->
				<view v-show="item.professorId && item.isEvaluate != 1"><button type="primary" size="mini"
						@click="toGowen(item)">调查问卷</button></view>
			</uni-card>
		</view>
	</view>
</template>

<script>
	import {
		getMyCourse
	} from '@/api/jz/student/info.js';
	import {
	
		getClassInfoByTeacherId
	} from '@/api/jz/course/api.js'

	export default {
		data() {
			return {
				list: [{
					name: '待上课',
					count: 0

				}, {
					name: '已上课'
				}],
				options1: [],
				title1: "班级",
				selCla: "",
				current: 0,
				courseList: [],
				courseList2: [],
				info: {
					lunar: true,
					range: true,
					insert: false,
					selected: []
				},
				courseInfo: "",
				selected: [],
			}
		},
		mounted() {
			
		},
		onShow() {
			this.getCourse();
		},
		methods: {
			/**
			 * 选中日期的触发事件
			 */
			dateChange: function(e) {
				var arr = []
				for (var i = 0; i < this.courseList2.length; i++) {
					if (this.courseList2[i].date == e.fulldate) {
						arr.push(this.courseList2[i])
					}
				}
				this.courseList = arr


			},
			changeCLass(e){
				
				var arr = []
				for (var i = 0; i < this.courseList2.length; i++) {
					if (this.courseList2[i].classCode == e) {
						arr.push(this.courseList2[i])
						this.title1 = this.courseList2[i].className
					}
				}
			
				this.courseList = arr
			},
			/**
			 *  选中月份的触发事件
			 */
			monthSwitch: function(e) {
				console.log(e);
			},
			open() {
				this.$refs.calendar.open();
			},
			close() {
				console.log('弹窗关闭');
			},
			changee(e) {
				console.log('change 返回:', e);
				// 模拟动态打卡
				console.log(this.info.selected);
				if (this.info.selected.length > 5) return;
				this.info.selected.push({
					date: e.fulldate,
					// info: '打卡'
				});
			},
			confirm(e) {
				console.log('confirm 返回:', e);
			},
			monthSwitch(e) {
				console.log('monthSwitchs 返回:', e);
			},
			toGowen(val) {
				var result = this.fun_date(7, val.quittingTime)
				if (result >= 0) {
					this.$modal.msgError("已超过一周，不能答题")
					return
				}
				uni.navigateTo({
					url: "/pages/questionnaire/index?courseId=" + val.id
				})
			},

			fun_date(aa, time) {

				var date1 = new Date(time);

				var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示当前时间

				var date2 = new Date(date1);

				date2.setDate(date1.getDate() + aa);

				var time2 = date2.getFullYear() + "-" +

					(date2.getMonth() + 1) + "-" + date2.getDate();



				var date3 = new Date();

				var timenow = date3.getFullYear() + "-" + (date3.getMonth() + 1) + "-" + date3.getDate();
				let date4 = new Date(timenow).getTime();
				let date5 = new Date(time2).getTime();
				var result = date4 - date5;
				return result;

			},

			change(index) {
				this.current = index;
			},
			deRepeat(arr){
			    let obj={};
			    arr.forEach((item,index)=>{
			        // 若重复则删除该项
			        if(obj.hasOwnProperty(item.label)){
			            arr.splice(index,1);
			        // 不重复则存入obj
			        }else{
			            obj[item.label]=item.value;
			        }
			    });
			    return arr;
			},
			async getCourse() {
				await getMyCourse(this.studentMyCourseVo).then((res) => {

					for (var i = 0; i < res.waitForClass.length; i++) {
						res.waitForClass[i].date = this.formatTime(res.waitForClass[i].attendClassTime)
						res.waitForClass[i].attendClassTime = "上课时间：" + res.waitForClass[i].attendClassTime;

						res.waitForClass[i].type = "abnormal";
						res.waitForClass[i].quittingTimeText = "下课时间：" + res.waitForClass[i].quittingTime;
						if (res.waitForClass[i].professorName) {
							res.waitForClass[i].title = res.waitForClass[i].title + "(" + res.waitForClass[i]
								.professorName + ")";
						}

					}

					for (var i = 0; i < res.quittingTime.length; i++) {
						res.quittingTime[i].date = this.formatTime(res.quittingTime[i].attendClassTime);
						res.quittingTime[i].attendClassTime = "上课时间：" + res.quittingTime[i].attendClassTime;

						res.quittingTime[i].type = "normal";
						res.quittingTime[i].quittingTimeText = "下课时间：" + res.quittingTime[i].quittingTime;
						if (res.quittingTime[i].professorName) {
							res.quittingTime[i].title = res.quittingTime[i].title + "(" + res.quittingTime[i]
								.professorName + ")";
						}

					}
					this.courseList = res.waitForClass.concat(res.quittingTime)
					this.courseList2 = res.waitForClass.concat(res.quittingTime)
					this.selected = res.waitForClass.concat(res.quittingTime)
					
					
					
					for (var i = 0; i < this.selected.length; i++) {
						this.options1.push({
							label: this.selected[i].className,
							value: this.selected[i].classCode
						})
						this.options1 = this.deRepeat(this.options1);
						
					}
				})
			},
			formatTime(str) {
				console.log(str)
				var y = new Date(str).getFullYear();
				var m = new Date(str).getMonth() + 1;
				if (m < 10) {
					m = "0" + m;
				}
				var d = new Date(str).getDate();
				if (d < 10) {
					d = "0" + d;
				}
				return y + '-' + m + '-' + d;
			}
		}
	}
</script>
<style scoped lang="scss"></style>
